package com.xin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xin.entity.LoginUser;
import com.xin.entity.UserAccountEntity;
import com.xin.entity.UserInfo;
import com.xin.mapper.UserAccountMapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    private UserAccountMapper userAccountMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("UserDetailServiceImpl");
        UserAccountEntity account = this.userAccountMapper.selectOne(new QueryWrapper<UserAccountEntity>().eq("user_account", username));

        if (account==null) {
            System.out.println("不存在信息");
            throw new RuntimeException("用户名或者密码错误");
        }
        //返回的作用是用于通过passwordEncode对比UserDetails的密码与Authentication中的密码是否一致
        UserInfo userInfo  = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(account.getUserPassword());
        return new LoginUser(userInfo);
    }
}
